#!/usr/bin/env python
# encoding: utf-8
"""
detector.py

Created by Dmitry Tsybin on 2011-03-18.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""

import sys
import os
import unittest
from constants import *
from cusum import *
from movingAverage import *
from threshold import *
from ttest import *

"""

"""
class Detector:
	'''Класс, запускающий все методы, голосование, и определяющий наличие разладки.'''
	
	def __init__(self):

		self.cusumRes = Cusum()
		self.movingAverageRes = MovingAverage()
		self.thresholdRes = Threshold()
		self.tTestRes = TTest()
	
		self.cusumStatus = STATUS.ok
		self.movingAverageStatus = STATUS.ok
		self.thresholdStatus = STATUS.ok
		self.tTestStatus = STATUS.ok
	
		#тут же вводятся и остальные методы
	
	def discord(self, nowValue):
		'''Функция голосования, решающая есть ли разладка, или нет.'''
		
		statusOk = 0
		statusDiscord = 0
		statusCrash = 0
		
		self.cusumStatus = self.cusumRes.discord(nowValue)
		self.movingAverageStatus = self.movingAverageRes.discord(nowValue)
		self.thresholdStatus = self.thresholdRes.discord(nowValue)
		self.tTestStatus = self.tTestRes.discord(nowValue)
		
		statuses = [self.cusumStatus, self.movingAverageStatus, self.thresholdStatus, self.tTestStatus]
		
		for status in statuses:
			if status == STATUS.ok:
				statusOk += 1
			elif status == STATUS.discord:
				statusDiscord += 1
			else:
				statusCrash += 1
		
		result = STATUS.ok
		
		if statusDiscord >= 2:
			result = STATUS.discord
		if statusCrash >=2:
			result = STATUS.crash
			
		return result
	
class DetectorTest(unittest.TestCase):
	knownValuesDiscord = [ ([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09], STATUS.ok),	
	 					([1, 2, 3, 4, 5, 6, 7, 8, 9], STATUS.crash),
						([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 1, 1, 0.1], STATUS.ok),
						([ 0 ], STATUS.ok) ]							

	def testDiscord1(self):
		for listVals, status in self.knownValuesDiscord:
			testUnit = Detector()
			for nowVal in listVals:
				result = testUnit.discord(nowVal)
			self.assertEqual(status, result)
			
if __name__ == '__main__':
	unittest.main()